package com.qf.ssm.service.impl;

import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarOrderMapper;
import com.qf.ssm.mapper.CarRentalCertificateMapper;
import com.qf.ssm.mapper.PickUpInformationMapper;
import com.qf.ssm.po.*;
import com.qf.ssm.qo.CarRentalCertificateQo;
import com.qf.ssm.qo.OrderStatusQO;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarOrderService;
import com.qf.ssm.service.CarRentalCertificateService;

import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.CarReturnInformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄梓濠
 * @Create 2024-01-05-14:53
 */
@Service
public class CarRentalCertificateServiceImpl implements CarRentalCertificateService {
    @Autowired
   private CarRentalCertificateMapper carRentalCertificateMapper ;
    @Autowired
    private PickUpInformationMapper pickUpInformationMapper;
    @Autowired
    private CarOrderService carOrderService;
    @Override
    public CarReturnInformationVo getCarRental(Integer orderId) {
        //判断是否存在原始数据 不存在即增加
        Integer count1 = 0;
        Integer count = null;
        try {
            count1 = carRentalCertificateMapper.selectCarRentalByOrderId(orderId);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (count1 == 0){
            //添加原有的数据
            try {
                count = carRentalCertificateMapper.insertCarRentalCertificate(orderId);
            } catch (ServiceException e) {
                throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
            }
            if (count < 1){
                throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
            }
        }

        //查询原有的数据
        CarReturnInformation carReturnInformation = null;
        CarReturnInformationVo carReturnInformationVo = null;
        try {
            carReturnInformation = carRentalCertificateMapper.selectCarRentalCertificate(orderId);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        carReturnInformationVo = CommonBeanutils.convertTo(carReturnInformation, CarReturnInformationVo::new);
        //查询所有员工
        List<Emp> emps = pickUpInformationMapper.selectAllEmp();
        //查询还车时的车辆损耗状态
        List<VehicleCondition> vehicleConditions = carRentalCertificateMapper.selectAllVehicleCondition();
        //给vo对象赋值
        carReturnInformationVo.setEmp(emps);
        carReturnInformationVo.setVehicleCondition(vehicleConditions);
        return carReturnInformationVo;
    }



    /**
     *
     * @param carRentalCertificateQo returnId 与OrderId为必要参数
     * @return
     */
    //添加还车单后来写入的数据并把订单状态设置为已完成 根据returnId修改
    @Transactional
    @Override
    public Integer carRentalFinish(CarRentalCertificateQo carRentalCertificateQo) {
        Integer orderId = carRentalCertificateQo.getOrderId();

        Integer count = null;
        try {
            count = carRentalCertificateMapper.updateCarRentalCertificate(carRentalCertificateQo);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }
        if (count <1){
            throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }

        //更改订单状态为已完成
        OrderStatusQO orderStatusQO = new OrderStatusQO();
        orderStatusQO.setOrderId(new Integer[]{orderId});
        orderStatusQO.setOrderStatusId(3);
        Integer count1 =null;
        try {
           count1 = carOrderService.editOrderStatus(orderStatusQO);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }
        if (count1 < 1){
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }

        return count;
    }
}
